package com.cskaoyan.service.impl;

import com.cskaoyan.bean.HistoryParameter;
import com.cskaoyan.bean.PageData;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.model.MarketSearchHistory;
import com.cskaoyan.model.MarketSearchHistoryExample;
import com.cskaoyan.service.HistoryService;
import com.cskaoyan.utils.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class HistoryServiceImpl implements HistoryService {
    @Override
    public PageData list(HistoryParameter historyParameter) {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketSearchHistoryMapper mapper = sqlsession.getMapper(MarketSearchHistoryMapper.class);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        //分页
        Integer page = historyParameter.getPage();
        Integer limit = historyParameter.getLimit();
        PageHelper.startPage(page, limit);
        //排序
        String sort = historyParameter.getSort();
        String order = historyParameter.getOrder();
        marketSearchHistoryExample.setOrderByClause(sort + " " + order);
        //条件
        Integer userId = historyParameter.getUserId();
        if (userId != null && userId != 0) {
            criteria.andUserIdEqualTo(userId);
        }
        String keyword = historyParameter.getKeyword();
        if (keyword != null && !keyword.equals("")) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        List<MarketSearchHistory> marketSearchHistories = mapper.selectByExample(marketSearchHistoryExample);

        sqlsession.commit();
        sqlsession.close();
        return PageData.list(marketSearchHistories);
    }
}
